#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 50005;

int n, m;
struct Cow {
  int a, b, id;
  bool operator<(const Cow& o) const { return a < o.a; }
} cows[N];
int ans[N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cows[i].id = i, cin >> cows[i].a >> cows[i].b;
  sort(cows + 1, cows + n + 1);
  int nxt = 1;
  priority_queue<pii, vector<pii>, greater<pii>> work;
  vector<int> idle;
  rep(i, 1, n) {
    int a = cows[i].a, b = cows[i].b;
    int id = cows[i].id;
    while (work.size() && work.top().first < a) {
      idle.push_back(work.top().second);
      work.pop();
    }
    int now = 0;
    if (idle.empty())
      now = nxt++;
    else {
      now = idle.back();
      idle.pop_back();
    }
    work.push(pii(b, now));
    ans[id] = now;
  }
  cout << nxt - 1 << endl;
  rep(i, 1, n) cout << ans[i] << endl;
  return 0;
}